﻿/*
 * Created by SharpDevelop.
 * User: Jack Brinegar
 * Date: 1/29/2013
 * Time: 7:35 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;
using System.Xml.Serialization;

namespace AdminDesktop
{
	/// <summary>
	/// Description of DataEngine.
	/// </summary>
	static class DataEngine
	{
		public static ButternutSkiPatrol.DataFile dataFile = null;
		
		public static ButternutSkiPatrol.AppSettings APP_CONFIG = null;
		public static string _appConfigFilename = string.Empty ;
		public static string DataFileName = string.Empty;
		
		public static string LastError = string.Empty;
		
		public static string LoadRosterFile(string FileName)
		{
			DataFileName = FileName;
			
			//this will load the data when the program is initialized
			//string xmlFile = @"C:\Projects\ButternutSkiPatrol\AdminDesktop\DataFile.xml";
			string xmlFile = DataFileName;
			string myReturn = "Loading the data file '" + xmlFile + "'...";
			try
			{
				//Load the dataFile
				// Create an instance of the XmlSerializer.
				XmlSerializer serializer = new XmlSerializer(typeof(ButternutSkiPatrol.DataFile));
	      		// Reading the XML document requires a FileStream.
	      		Stream reader= new FileStream(xmlFile,FileMode.Open);
	      		dataFile = (ButternutSkiPatrol.DataFile) serializer.Deserialize(reader);
	      		reader.Close();
	      		BackupRosterFile();
	      		myReturn = "Roster file has been loaded successfully";
			}
			catch(Exception ex)
			{
				//MessageBox.Show(ex.Message,"Adminstrative Desktop");
				myReturn = ex.Message;
				LastError = ex.Message;
			}	
			return myReturn;
		}
		
		public static void DisposeOfRoster()
		{
			dataFile=null;
			//APP_CONFIG.LastRosterFile="";
		}
		
		public static void CreateNewRosterFile(string FileName)
		{
			//if(IsDirty)
			//{
				//save the current file first...
				SaveRosterFile();
			//}
			
			//create the new file...
			DataFileName=FileName;
			ButternutSkiPatrol.DataFile _newFile = new ButternutSkiPatrol.DataFile();
			dataFile = _newFile;
			SaveRosterFile();
		}
		
		public static bool BackupRosterFile()
		{
			bool myReturn = false;
			try
			{
				System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(dataFile.GetType());
				System.IO.StreamWriter sw = new StreamWriter(APP_CONFIG.BackupDataDirectory + @"\" + DateTime.Now.ToFileTime() + ".xml");
				x.Serialize(sw,dataFile);
				sw.Close();
			}
			catch(Exception ex)
			{
				LastError = ex.Message;
			}
			return myReturn;			
		}
		
		public static bool SaveRosterFile()
		{
			bool myReturn = false;
			try
			{
				System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(dataFile.GetType());
				System.IO.StreamWriter sw = new StreamWriter(DataFileName);
				x.Serialize(sw,dataFile);
				sw.Close();
				APP_CONFIG.LastRosterFile=DataFileName;
				SaveAppConfig();
				myReturn = true;
			}
			catch(Exception ex)
			{
				LastError = ex.Message;
			}

			return myReturn;
		}
		
		public static bool AddPatroller(ButternutSkiPatrol.Person patroller)
		{
			bool myReturn = false;
			//check for a roster...create a roster if needed
			try
			{
				dataFile.roster.Patrollers.Add(patroller);
				myReturn = true;
				
				SaveRosterFile();
			}
			catch(Exception ex1)
			{
				LastError = ex1.Message;
				try
				{
					//assumption is the list did not exist
					ButternutSkiPatrol.Roster roster = new ButternutSkiPatrol.Roster();
					dataFile.roster=roster;
					roster.Patrollers = new System.Collections.Generic.List<ButternutSkiPatrol.Person>();
					roster.Patrollers.Add(patroller);
					myReturn = true;
					
					SaveRosterFile();
				}
				catch(Exception ex2)
				{
					//something really went wrong...
					LastError = ex1.Message + "\r\n" + ex2.Message;
					myReturn = false;
					
				}
			}
			return myReturn;
		}
		
		public static Stream GetDataStream()
		{
		
				
			System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(dataFile.GetType());
			MemoryStream xmlStream = new MemoryStream( );
			x.Serialize(xmlStream,dataFile);
			
			xmlStream.Flush();//Adjust this if you want read your data 
			xmlStream.Position = 0;
			return xmlStream;

		}
		
		#region AppConfig
		public static string LoadAppConfig()
		{
			_appConfigFilename = AppDomain.CurrentDomain.BaseDirectory + @"program.cfg";
			
			string xmlFile = DataFileName;
			string myReturn = "Loading the application configuration...";
			try
			{
				//Load the dataFile
				// Create an instance of the XmlSerializer.
				XmlSerializer serializer = new XmlSerializer(typeof(ButternutSkiPatrol.AppSettings));
	      		// Reading the XML document requires a FileStream.
	      		Stream reader= new FileStream(_appConfigFilename,FileMode.Open);
	      		APP_CONFIG = (ButternutSkiPatrol.AppSettings) serializer.Deserialize(reader);
	      		reader.Close();
	      		
	      		//BackupRosterFile();
	      		myReturn = "System has been initialized";
			}
			catch(Exception ex)
			{
				//no cfg file...make a default?
				APP_CONFIG= new ButternutSkiPatrol.AppSettings();
				APP_CONFIG.UseTitleCase=true;
				APP_CONFIG.DataDirectory = AppDomain.CurrentDomain.BaseDirectory;
				APP_CONFIG.DataDirectory.ToLower().Replace(@"debug\","");
				APP_CONFIG.DataDirectory.ToLower().Replace(@"bin\","");
				APP_CONFIG.DataDirectory += @"data\";
				SaveAppConfig();
			}	
			return myReturn;
		}
		
		public static bool SaveAppConfig()
		{
			bool myReturn = false;
			try
			{
				System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(APP_CONFIG.GetType());
				System.IO.StreamWriter sw = new StreamWriter(_appConfigFilename);
				x.Serialize(sw,APP_CONFIG);
				sw.Close();
				myReturn = true;
			}
			catch(Exception ex)
			{
				LastError = ex.Message;
			}

			return myReturn;
		}
		
		#endregion
	}
}
